import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'z_container.dart';

class ZText extends StatelessWidget {
  /// text相关
  final String? text;
  final Color textColor;
  final double size;
  final TextOverflow? overflow;
  final FontWeight fontWeight;
  final int? maxLines;
  final TextAlign? textAlign;

  final List<String>? richText;
  final List<Color>? richColor;
  final Map<int, ZTextStyle>? richStyle;

  /// 背景相关
  final Color? color;

  // final Color? sideColor;
  // final double? sideWidth;
  final double? width;
  final double? height;
  final double? radius;
  final EdgeInsets? margin;
  final EdgeInsets? padding;
  final Alignment? alignment;
  final BorderSide? side;

  /// 点击相关
  final void Function()? onTap;
  final bool inkWell;

  const ZText({
    super.key,
    this.text,
    this.textColor = Colors.black,
    this.size = 14,
    this.fontWeight = FontWeight.normal,
    this.overflow = TextOverflow.ellipsis,
    this.maxLines = 1,
    this.textAlign,
    this.onTap,
    this.inkWell = false,
    this.color,
    this.height,
    this.width,
    this.radius,
    this.padding,
    this.margin,
    this.alignment,
    this.side,
  })  : richColor = null,
        richStyle = null,
        richText = null;

  const ZText.rich({
    super.key,
    this.richText,
    this.richStyle,
    this.richColor,
    this.textColor = Colors.black,
    this.size = 14,
    this.fontWeight = FontWeight.normal,
    this.overflow,
    this.maxLines,
    this.textAlign,
    this.onTap,
    this.inkWell = false,
    this.color,
    this.height,
    this.width,
    this.radius,
    this.padding,
    this.margin,
    this.alignment,
    this.side,
  }) : text = null;

  bool get _isContainer =>
      color != null || width != null || height != null || padding != null || margin != null || alignment != null;

  @override
  Widget build(BuildContext context) {
    Widget target;
    if (richText == null) {
      target = _text();
    } else {
      target = _richText();
    }
    if (_isContainer) {
      target = ZContainer(
        width: width,
        height: height,
        margin: margin,
        padding: padding,
        alignment: alignment,
        color: color,
        radius: radius ?? 0,
        side: side,
        child: target,
      );
    }
    if (onTap != null) {
      if (inkWell) {
        target = InkWell(onTap: onTap, child: target);
      } else {
        target = GestureDetector(onTap: onTap, behavior: HitTestBehavior.opaque, child: target);
      }
    }
    return target;
  }

  Widget _text() {
    return Text(
      text ?? '',
      style: TextStyle(color: textColor, fontSize: size.r, fontWeight: fontWeight),
      overflow: overflow,
      maxLines: maxLines,
      textAlign: textAlign,
    );
  }

  Widget _richText() {
    var children = List.generate(
      richText!.length,
      (index) {
        var style = richStyle?[index];
        style ??= ZTextStyle(color: richColor?[index]);
        return TextSpan(
          text: richText![index],
          style: TextStyle(color: style.color, fontSize: style.size, fontWeight: style.fontWeight),
          recognizer: style._tapGestureRecognizer,
        );
      },
    );
    return Text.rich(
      TextSpan(children: children),
      style: TextStyle(color: textColor, fontSize: size.r, fontWeight: fontWeight),
      overflow: overflow,
      maxLines: maxLines,
      textAlign: textAlign,
    );
  }
}

class ZTextStyle {
  final Color? color;
  final double? size;
  final FontWeight? fontWeight;
  final TapGestureRecognizer? _tapGestureRecognizer;

  ZTextStyle({this.fontWeight, this.size, this.color, void Function()? onTap})
      : _tapGestureRecognizer = onTap == null ? null : (TapGestureRecognizer()..onTap = onTap);
}
